<template>
    <div class="price-page">
        <h1 class="title">{{ t('Price.Title') }}</h1>
        <p class="subtitle">{{ t('Price.Subtitle') }}</p>
        <p class="credit-info">{{ t('Price.CreditInfo') }}</p>

        <div class="price-cards">
            <!-- 免费版 -->
            <div class="price-card free">
                <div class="card-header">
                    <h2>{{ t('Price.Free.Title') }}</h2>
                    <div class="price">
                        <span class="currency">¥</span>
                        <span class="amount">0</span>
                    </div>
                    <p class="period">{{ t('Price.Free.Period') }}</p>
                </div>
                <button class="action-btn free-btn">{{ t('Price.Free.Button') }}</button>
                <ul class="features">
                    <li>{{ t('Price.Free.Features.Credits') }}</li>
                    <li>{{ t('Price.Free.Features.AI') }}</li>
                    <li>{{ t('Price.Free.Features.TextRemoval') }}</li>
                    <li>{{ t('Price.Free.Features.Manual') }}</li>
                </ul>
            </div>

            <!-- 专业套餐 -->
            <div class="price-card pro featured">
                <div class="card-header">
                    <h2>{{ t('Price.Pro.Title') }}</h2>
                    <div class="price">
                        <span class="currency">$</span>
                        <span class="amount">{{ proPack.price }}</span>
                        <span class="period">{{ t('Price.Pro.PerMonth') }}</span>
                    </div>
                    <!-- <label class="toggle">
                        <input type="checkbox" v-model="isYearly" />
                        <span class="slider">{{ t('Price.Pro.YearlyDiscount') }}</span>
                    </label> -->
                </div>
                <button class="action-btn" @click="payTask(proPack)">
                    {{ t('Price.Pro.Button', {credits: proPack.jifen})}}
                </button>
                <!-- <div class="credits">
                    <div class="credit-option" v-for="credit in creditOptions" :key="credit.points">
                        <span class="points">{{ credit.points }}积分</span>
                        <span class="price">¥{{ credit.price }}</span>
                    </div>
                </div> -->
                <ul class="features">
                    <li>{{t('Price.Pro.Features.Credits')}} {{ cost }}{{ t('Price.Pro.Features.CreditsUnit')}}，{{ t('Price.Pro.Features.FreeBenefitsDesc') }}</li>
                    <li>{{ t('Price.Pro.Features.FreeBenefits') }}</li>
                    <li>{{ t('Price.Pro.Features.Quality') }}</li>
                    <li>{{ t('Price.Pro.Features.Batch') }}</li>
                    <li>{{ t('Price.Pro.Features.NoAds') }}</li>
                    <li>{{ t('Price.Pro.Features.Crypto') }}</li>
                </ul>
                <p class="note">{{ t('Price.Pro.Note') }}</p>
            </div>

            <!-- 按量付费 -->
            <div class="price-card pay-as-you-go">
                <div class="card-header">
                    <h2>{{ t('Price.PayAsYouGo.Title') }}</h2>
                    <h3>{{ t('Price.PayAsYouGo.Subtitle') }}</h3>
                </div>
                <div class="credit-tiers">
                    <div class="tier" v-for="tier in payAsYouGoTiers" :key="tier.id"
                        :class="{ active: selectedCredits.id === tier.id }" @click="selectCredits(tier)">
                        <div class="credit-box">
                            <span class="credits">{{ tier.jifen }}</span>
                            <span class="label">积分</span>
                        </div>
                        <span class="price">${{ tier.price }}</span>
                    </div>
                </div>
                <button class="action-btn one-buy" @click="payOneBuy">{{ t('Price.PayAsYouGo.Button', { credits: selectedCredits.jifen })
                    }}</button>
                <ul class="features">
                    <li>{{ t('Price.Pro.Features.Credits') }} {{ cost }}{{ t('Price.Pro.Features.CreditsUnit') }}</li>
                    <li>{{ t('Price.PayAsYouGo.Features.Permanent') }}</li>
                    <li>{{ t('Price.PayAsYouGo.Features.AllFeatures') }}</li>
                    <li>{{ t('Price.PayAsYouGo.Features.Flexible') }}</li>
                    <li>{{ t('Price.PayAsYouGo.Features.Payment') }}</li>
                </ul>
                <p class="note">{{ t('Price.PayAsYouGo.Note') }}</p>
            </div>

            <!-- 企业版 -->
            <!-- <div class="price-card enterprise">
                <div class="card-header">
                    <h2>{{ t('Price.Enterprise.Title') }}</h2>
                    <h3>{{ t('Price.Enterprise.Subtitle') }}</h3>
                    <p>{{ t('Price.Enterprise.Contact') }}</p>
                </div>
                <button class="action-btn contact-btn">{{ t('Price.Enterprise.Button') }}</button>
                <ul class="features">
                    <li>{{ t('Price.Enterprise.Features.Unlimited') }}</li>
                    <li>{{ t('Price.Enterprise.Features.API') }}</li>
                    <li>{{ t('Price.Enterprise.Features.Support') }}</li>
                    <li>{{ t('Price.Enterprise.Features.Team') }}</li>
                    <li>{{ t('Price.Enterprise.Features.Custom') }}</li>
                    <li>{{ t('Price.Enterprise.Features.Manager') }}</li>
                </ul>
                <p class="note">{{ t('Price.Enterprise.Note') }}</p>
            </div> -->
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { getVariantListApi, payTaskApi } from '@/api/payment'
import encrypt from '@/utils/encrypt.js'
const { t } = useI18n()
const cost = ref(0)
const isYearly = ref(false)
const userInfo = encrypt.GetLocalData('userInfo')
const selectedCredits = ref({}) // 默认选中500积分
const proPack = ref({})
const storeId = ref(0)
const creditOptions = [
    { points: 1200, price: '48.00' },
    { points: 2400, price: '96.00' },
    { points: 6000, price: '240.00' },
    { points: 12000, price: '480.00' }
]

const payAsYouGoTiers = ref([
    // { credits: 500, price: '15.00' },
    // { credits: 1000, price: '20.00' },
    // { credits: 3000, price: '90.00' },
    // { credits: 5000, price: '150.00' }
])

const selectCredits = credits => {
    selectedCredits.value = credits
}
const getVariantList = async () => {
    const res = await getVariantListApi()
    cost.value = res.data.cost;
    storeId.value = res.data.store_id
    res.data.variants.forEach(item => {
        item.price = (item.price / 100).toFixed(2)
        if (item.is_subscription == false) {
            payAsYouGoTiers.value.push(item)
        } else {
            proPack.value = item
        }
    })
    console.log(cost.value, 'cost.value');
    console.log(storeId.value, 'storeId.value');
    
}

const payOneBuy = async () => {
    if (selectedCredits.value?.id > 0) { 
        payTask(selectedCredits.value)
    } else {
        ElMessage.error('请选择积分')
    }
}

const payTask = async (taskItem) => {
    let params = {
        "data": {
            "type": "checkouts",
            "attributes": {
                "custom_price": '',
                "product_options": {
                    "enabled_variants": [taskItem.id]
                },
                "checkout_options": {
                    "button_color": "#7047EB"
                },
                "checkout_data": {
                    "custom": {
                        "user_id":userInfo.id + '', "jifen": `${taskItem.jifen}`, "is_subscription": `${taskItem.is_subscription}`
                    }
                },

                "preview": false
            },
            "relationships": {
                "store": {
                    "data": {
                        "type": "stores",
                        "id": storeId.value
                    }
                },
                "variant": {
                    "data": {
                        "type": "variants",
                        "id": taskItem.id
                    }
                }
            }
        }
    }
    const res = await payTaskApi(params)
    console.log(res)
    if(res.code == 200){
        window.open(res.data.data.attributes.url,'_target')
    }
}

onMounted(() => {
    getVariantList()
})
</script>

<style lang="scss" scoped>
.price-page {
    height: calc(100% - 2px);
    padding: 40px 20px;
    margin: 0 auto;
    color: #000;
    text-align: center;
    background-color: #fff;
    margin-top: 2px;
    box-sizing: border-box;

    .title {
        font-size: 32px;
        font-weight: bold;
        margin-bottom: 16px;
        color: #333;
    }

    .subtitle {
        font-size: 18px;
        color: #666;
        margin-bottom: 8px;
    }

    .credit-info {
        font-size: 16px;
        color: #888;
        margin-bottom: 40px;
    }

    .price-cards {
        display: flex;
        gap: 20px;
        justify-content: center;
        flex-wrap: wrap;

        .price-card {
            flex: 1;
            min-width: 280px;
            max-width: 320px;
            padding: 24px;
            border-radius: 12px;
            background: #fff;
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);

            &.featured {
                border: 2px solid #ff5722;
                transform: scale(1.02);
            }

            .card-header {
                margin-bottom: 24px;

                h2 {
                    font-size: 24px;
                    margin-bottom: 16px;
                }

                .price {
                    font-size: 36px;
                    font-weight: bold;
                    margin-bottom: 8px;

                    .currency {
                        font-size: 24px;
                    }

                    .period {
                        font-size: 16px;
                        color: #666;
                    }
                }
            }

            .action-btn {
                width: 100%;
                padding: 12px;
                border-radius: 6px;
                border: none;
                background: #ff5722;
                color: white;
                font-size: 16px;
                font-weight: bold;
                cursor: pointer;
                margin-bottom: 24px;

                &.free-btn {
                    background: #4caf50;
                }

                &.contact-btn {
                    background: #2196f3;
                }
            }

            .one-buy {
                background: #4285f4;
            }

            .features {
                list-style: none;
                padding: 0;
                margin: 0;

                li {
                    padding: 8px 0;
                    color: #666;
                    text-align: left;
                    position: relative;
                    padding-left: 24px;

                    &:before {
                        content: '✓';
                        position: absolute;
                        left: 0;
                        color: #4caf50;
                    }
                }
            }

            .credits,
            .credit-tiers {
                margin: 16px 0;
                display: grid;
                gap: 8px;

                .credit-option,
                .tier {
                    display: flex;
                    justify-content: space-between;
                    padding: 8px;
                    border: 1px solid #eee;
                    border-radius: 4px;

                    &:hover {
                        background: #f9f9f9;
                    }
                }
            }

            .note {
                font-size: 14px;
                color: #888;
                margin-top: 16px;
            }

            .credit-tiers {
                display: flex;
                gap: 8px;
                margin: 16px 0;

                .tier {
                    flex: 1;
                    cursor: pointer;
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    gap: 4px;
                    padding: 8px 4px;
                    border-radius: 8px;
                    border: 1px solid #eee;
                    transition: all 0.3s ease;

                    &.active {
                        background: #4285f4;
                        border-color: #4285f4;

                        .credit-box {

                            .credits,
                            .label {
                                color: #fff;
                            }
                        }

                        .price {
                            color: #fff;
                        }
                    }

                    .credit-box {
                        display: flex;
                        flex-direction: column;
                        align-items: center;

                        .credits {
                            font-size: 16px;
                            font-weight: bold;
                            color: #333;
                        }

                        .label {
                            font-size: 12px;
                            color: #666;
                        }
                    }

                    .price {
                        font-size: 14px;
                        color: #666;
                    }
                }
            }
        }
    }

    .toggle {
        display: inline-block;
        position: relative;

        input {
            display: none;

            &:checked+.slider {
                background: #4caf50;
            }
        }

        .slider {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 16px;
            background: #ddd;
            color: white;
            cursor: pointer;
            transition: background 0.3s;
        }
    }
}
</style>
